public class MySingleList implements IList{
    static class ListNode{
        public int val;
        public ListNode next;
        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    @Override
    public void addFirst(int data) {
        ListNode node=new ListNode(data);
        node.next=head;
        head=node;
    }

    @Override
    public void addLast(int data) {
        ListNode node=new ListNode(data);
        if(head==null){
            node.next=head;
            head=node;
        }
        ListNode cur=head;
        while (cur.next!=null){
            cur=cur.next;
        }
        cur.next=node;
    }

    @Override
    public void addIndex(int index, int data) {
        if(index<0||index>size()){
            System.out.println("index位置不合法");
            return;
        }
        if(index==0){
            addLast(data);
            return;
        }else if(index==size()){
            addLast(data);
            return;
        }else {
            ListNode cur=head;
            ListNode node=new ListNode(data);
            while(index-1!=0){
                cur=cur.next;
                index--;
            }
            node.next=cur.next;
            cur.next=node;
        }
    }
    @Override
    public boolean contains(int key) {
        ListNode cur=head;
        while (cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        ListNode cur=head;
        if(cur==null){
            return;
        }
        if(cur.val==key){
            cur.next=head;
            return;
        }
        while (cur.next!=null){
            if(cur.next.val==key){
                ListNode del=cur.next;
                cur.next=del.next;
                return;
        }
            cur=cur.next;
        }
    }
    @Override
    public void removeAllkey(int key) {
        ListNode cur=head.next;
        ListNode perv=head;
        if(cur==null){
            return;
        }
        while (cur!=null){
            if(cur.val==key){
                perv.next=cur.next;
                cur=cur.next;
            }else {
                perv=cur;
                cur=cur.next;
            }
        }
        if(head.val==key){
            head=head.next;
        }
    }

    @Override
    public int size() {
        ListNode cur=head;
        int ret=0;
        if(cur==null){
            return 0;
        }else {
            while (cur!=null){
                ret++;
                cur=cur.next;
            }
        }
        return ret;
    }

    @Override
    public void clear() {
        head=null;
    }

    @Override
    public void display() {
        ListNode cur=head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }
    public void createList(){
        ListNode node1=new ListNode(1);
        ListNode node2=new ListNode(2);
        ListNode node3=new ListNode(3);
        ListNode node4=new ListNode(4);
        ListNode node5=new ListNode(5);
        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        node4.next=node5;
        this.head=node1;
    }
}
